package com.Practice;

import java.io.IOException;
import java.net.*;
import java.util.Scanner;

public class ServiceTest {
    public static void main(String[] args) {
        System.out.println("服务端启动");
        try ( DatagramSocket socket = new DatagramSocket(8888)){
            Scanner sc = new Scanner(System.in);
            byte[] bytes = new byte[1024];
            while (true) {
                InetAddress address =InetAddress.getByAddress(new byte[]{10,0,3,-48});
                DatagramPacket packet = new DatagramPacket(bytes,0, bytes.length,address,7788);
                socket.receive(packet);
                String receiveString = new String(packet.getData(),0, packet.getLength(),"UTF-8");
                System.out.println(packet.getAddress() + "发送了：" + receiveString);

                //服务端回复消息
                System.out.println("请回复：");
                String string = sc.nextLine();
                byte[] bytes1 = string.getBytes("UTF-8");
                DatagramPacket sendPacket = new DatagramPacket(bytes1,0,bytes1.length,packet.getAddress(), 7788);
                socket.send(sendPacket);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}